package com.team.arms.xitong.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.team.arms.common.Result;
import com.team.arms.common.ry.core.domain.AjaxResult;
import com.team.arms.common.util.ConvertUtil;
import com.team.arms.utils.PageUtil;
import com.team.arms.xitong.entity.DicDatail;
import com.team.arms.xitong.entity.DicType;
import com.team.arms.xitong.entity.dto.DictDto;
import com.team.arms.xitong.entity.dto.DictUpdateDto;
import com.team.arms.xitong.entity.vo.DictVo;
import com.team.arms.xitong.mapper.DicDatailMapper;
import com.team.arms.xitong.mapper.DicTypeMapper;
import com.team.arms.xitong.service.IDicDatailService;
import com.team.arms.xitong.service.IDicTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Huazige
 * @since 2022-06-24
 */
@RestController
@RequestMapping("/dic")
@CrossOrigin
@Api(tags = "系统管理-字典管理")
public class DicTypeController {
    @Autowired
    DicTypeMapper dicTypeMapper;
    @Autowired
    IDicTypeService  iDicTypeService;
    @Autowired
    DicDatailMapper dicDatailMapper;
    @Autowired
    IDicDatailService iDicDatailService;
    @Autowired
    ConvertUtil convertUtil;

    @ApiOperation("分页显示所有字典信息")
    @GetMapping(value = "/list/{pageNum}/{pageSize}")
    public Result selectMsg(
            @ApiParam("当前页数")       @PathVariable("pageNum")Integer pageNum,
            @ApiParam("每页的数据量")       @PathVariable("pageSize")Integer pageSize

    ){
        List<DictVo> dicTypes = dicTypeMapper.select();

//        List<UserVo>  userVoList=  convertUtil.convert(dicTypes, UserVo.class);

        //转换效率的问题,直接sql查询返回这个类型效率相对高一些
        Page page = PageUtil.list2Page(dicTypes, pageNum, pageSize);
        return page !=null ? Result.success("分页成功").data(page): Result.error("分页失败");
    }
    @ApiOperation("单个+批量删除字典信息")
    @DeleteMapping("/deletelist")
    public Result deleteInformation(
            @ApiParam("字典详细名称编号") @RequestParam("dicDetailId")List<String> deletelist ){
        boolean b = iDicDatailService.removeByIds(deletelist);

       /* for ( String id:deletelist
        ) {
            QueryWrapper<DicType> wrapper = new QueryWrapper();
            wrapper.eq("dic_id",id);
            DicType dicType = dicTypeMapper.selectOne(wrapper);
            String dicTypeId = dicType.getDicTypeId();
            dicTypeMapper.delete(wrapper);



            QueryWrapper<DicDatail> wrapper2 = new QueryWrapper();
            wrapper2.eq("dic_type_id",dicTypeId);
            dicDatailMapper.delete(wrapper2);


        }
*/

        return b ? Result.success("批量删除成功") : Result.error("批量删除失败");

    }

    @ApiOperation("修改字典信息")
    @PutMapping("/update")
    public AjaxResult updateSchedule(@RequestBody DictUpdateDto dictUpdateDto){

        boolean b =   dicTypeMapper.update(dictUpdateDto);//前新后旧
        return b ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
    }


    @ApiOperation("新增字典信息")
    @PostMapping("/install")
    @Transactional
    public Result install(@RequestBody DictDto dictDto){
        String dicTypeName1 = dictDto.getDicTypeName();
        String dicDetailName = dictDto.getDicDetailName();
        QueryWrapper<DicType> wrapper2 = new QueryWrapper();
        wrapper2.eq("dic_type_name",dicTypeName1);
        DicType dicType1 = dicTypeMapper.selectOne(wrapper2);
        if (dicType1!=null){
            DicDatail dicDatail = new DicDatail();
            dicDatail.setDicTypeId(dicType1.getDicTypeId());
            dicDatail.setDicDetailName(dicDetailName);

            dicDatailMapper.insert(dicDatail);
            return Result.success("添加成功").data(dicType1).data(dicDatail);
        }
        DicType dicType = new DicType();
        String s = dicTypeMapper.getdicTypemax();
        dicType.setDicTypeName(dicTypeName1);
        dicType.setDicTypeId(s);
//        dicType.setDicTypeId(dicTypeId);

        dicTypeMapper.insert(dicType);
//        String dicTypeId = dicType.getDicTypeId();
//        QueryWrapper<DicType> wrapper3 = new QueryWrapper();
//        wrapper2.eq("dic_id",dicType.getDicId());
//
//        dicType.setDicTypeId(dicTypeId);
//        dicTypeMapper.update(dicType,wrapper3);


        DicDatail dicDatail = new DicDatail();
//        dicDatail.setDicTypeId(dicTypeId);
        dicDatail.setDicDetailName(dicDetailName);
        dicDatail.setDicTypeId(s);
        dicDatailMapper.insert(dicDatail);

         return Result.success("添加成功").data(dicType).data(dicDatail);

    }


}

